
package artistic;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */
public class ToolBox extends JPanel{

    private class ToolButton extends JButton implements ActionListener
    {
        private PaintTool tool;
        private ToolBox owner;
        ToolButton(ToolBox owner, Icon icon, PaintTool tool)
        {
            super(icon);
            this.owner = owner;
            this.tool = tool;
            this.addActionListener(this);
            setVisible(true);
        }

        /**
         * Get associated paint tool.
         *
         * @return The associated paint tool.
         */
        public PaintTool getTool() { return tool; }

        /**
         * Called when button is pressed,
         * informs ToolBox of button press.
         *
         * @param e ActionEvent
         */
        public void actionPerformed(ActionEvent e) {
            System.out.println(this.toString() + " pressed.");
            owner.buttonPressed(this);
        }

        @Override
        public String toString()
        {
            return tool.toString();
        }
    }

    private ArrayList<ToolButton> buttons;

    private ToolButton activeButton;

    private ColorChooser colorChooser;

    private Brush activeBrush;
    
    public Brush getActiveBrush()
    {
        return activeBrush;
    }

    private int opacity = 255;
    
    public void setOpacity(int opacity)
    {
        this.opacity = opacity;
    }

    public int getOpacity()
    {
        return opacity;
    }

    public ToolBox()
    {
        setLayout(new FlowLayout(FlowLayout.CENTER, 0, 2));

        activeBrush = new SimpleBrush(this);

        addTool(new BrushTool(this));
        addTool(new AirbrushTool(this));
        addTool(new PickTool(this));
        addTool(new LineTool(this));
        
        colorChooser  = new ColorChooser();
        colorChooser.setVisible(true);

        setVisible(true);
    }

    public ColorChooser getColorChooser()
    {
        return colorChooser;
    }

    private void buttonPressed(ToolButton button)
    {
        activeButton = button;
    }

    /**
     * Get the currently active paint tool.
     *
     * @return Active paint tool.
     */
    public PaintTool getActiveTool()
    {
        if(activeButton == null)
            return null;
        return activeButton.getTool();
    }

    /**
     * Add a tool to the tool box,
     * with a specified image to use
     * as icon.
     */
    public void addTool(PaintTool tool)
    {
        ToolButton button = new ToolButton(this, tool.getIcon(), tool);
        this.add(button);

        if(activeButton == null)
            activeButton = button;
    }
}
